import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  HttpCode,
  Param,
  ParseIntPipe,
  ParseUUIDPipe,
  // ValidationPipe,
  UsePipes,
  Res,
  HttpStatus,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import {
  CreateDogDto,
  UpdateDogDto,
  createDogSchema,
} from './dto/create-dog.dto';
import { DogService } from './dog.service';
import { Dog } from './interfaces/dog.interface';
import { Response } from 'express';
import { log } from 'console';
import { ZodValidationPipe } from '../common/pipe/zod';
import { ValidationPipe } from '../common/pipe/validation.class.pipe';
import { RolesGuard } from '../common/guards/roles.guard';
import { Roles } from '../common/decorator/roles.decorator';
import { LoggingInterceptor } from '../common/interceptor/logging.interceptor';
import { User } from '../common/decorator/user.decorator';

@Controller('dog')
@UseInterceptors(LoggingInterceptor)
export class DogController {
  constructor(private dogService: DogService) {}
  private id: number = 1;
  /*
   * 类验证器 -- 不可以过滤多余字段
   */
  // @Post()
  // async create(@Body(new ValidationPipe()) createDogDto: CreateDogDto) {
  //   this.dogService.create({ id: this.id++, ...createDogDto });
  // }

  /**
   * zod验证管道 -- 可以过滤多余字段
   */
  @Post()
  @HttpCode(200)
  @Roles(['admin'])
  @UsePipes(new ZodValidationPipe(createDogSchema))
  async create(@Body() createDogDto: CreateDogDto) {
    this.dogService.create({ id: this.id++, ...createDogDto });
  }

  /**
   * 原始创建
   */
  // @Post()
  // @HttpCode(200)
  // async create(@Body() createDogDto: CreateDogDto) {
  //   this.dogService.create(createDogDto);
  // }

  @Get()
  // @UseGuards(RolesGuard)
  async findAll(): Promise<Dog[]> {
    return this.dogService.findAll();
  }

  @Get(':id')
  async findOne(@User('firstName') firstName: string) {
    console.log(`Hello ${firstName}`);

    // return this.dogService.findOne(id);
  }

  // 字符串转换整数用例
  // @Get(':id')
  // async findOne(@Param('id', new ParseIntPipe()) id) {
  //   return this.dogService.findOne(id);
  // }

  // @Get(':id')
  // findOne(@Param('id') id: string) {
  //   return `This action returns a #${id} cat`;
  // }

  // // findOne(@Param('id') id: number, @Res() res: Response) {
  // //   res.status(HttpStatus.OK).json([`This action returns a #${id} cat`]);
  // // }
  // async findOne(@Param('id', ParseIntPipe) id: number) {
  //   return this.dogService.findAll();
  // }

  // @Get(':uuid')
  // async findUUid(@Param('uuid', new ParseUUIDPipe()) uuid: string) {
  //   console.log("uuid: ",uuid);
  //   return this.dogService.findAll();
  // }

  @Put(':id')
  update(@Param('id') id: number, @Body() updateCatDto: UpdateDogDto) {
    return `This action updates a #${id} cat`;
  }

  @Delete(':id')
  remove(@Param('id') id: number) {
    return `This action removes a #${id} cat`;
  }
}
